﻿using Core.Infarastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;

namespace Core.Models
{
    /// <summary>
    /// Provide s logic of the main hero
    /// </summary>
    public class Hero
    {

        #region Properties
        /// <summary>
        /// Current position on the X
        /// </summary>
        public int CurrentPosition_X { get; set; }
        /// <summary>
        /// Current position on the Y
        /// </summary>
        public int CurrentPosition_Y { get; set; }

        /// <summary>
        /// Indicates if the hero is moving
        /// </summary>
        public bool IsMoving { get; set; }
        /// <summary>
        /// Indicates if the hero can move right
        /// </summary>
        public bool CantGoRight { get; set; }
        /// <summary>
        /// Indicates if the hero can move left
        /// </summary>
        public bool CantGoLeft { get; set; }
        /// <summary>
        /// Indicates that the hero can't move in both sides 
        /// </summary>
        public bool Stacked { get; set; }
        /// <summary>
        /// Is setted when hero go back after jumping, if another button wasn't pressed
        /// </summary>
        public bool GoBack { get; set; }
       
        public bool Fall { get; set; }

        public bool Killed { get; set; }

        public bool Jumping { get; set; }

        public int Score { get; set; }

        public bool DestroyStackInAir { get; set; }

        /// <summary>
        /// Setted the stack which is left to hero, when the hero cross it
        /// </summary>
        public Stack LeftStack { get; set; }
        /// <summary>
        /// Setted the stack which is right to hero, when the hero cross it
        /// </summary>
        public Stack RightStack { get; set; }
        #endregion Properties

        int localCounter = 0;
        #region Methods
        /// <summary>
        /// Provide a moving logic of hero
        /// </summary>
        /// <param name="buttonPressed"></param>
        public void Move(Enums.Keys buttonPressed)
        {
            if (localCounter==0)
            {
                IsMoving = true;
            }        
            if (localCounter == Constants.STACK_SIZE)
            {
                localCounter = 0;
                Stacked = true;
                IsMoving = false;
                if (buttonPressed == Enums.Keys.KeyUp)
                {
                    Jumping = false;
                    GoBack = true;
                }
                return;
                
            }
           
            
            if (buttonPressed == Enums.Keys.KeyRight)
            {
                localCounter++;
                HeroMoveRight();
                CantGoLeft = false;
                LeftStack = null;

            }
            if (buttonPressed == Enums.Keys.KeyLeft)
            {
                localCounter++;
                HeroMoveLeft();
                CantGoRight = false;
                RightStack = null;
            }
            if (buttonPressed == Enums.Keys.KeyUp)
            {
                localCounter++;
                this.JumpUp();
            }
        }
        
        private void HeroMoveRight()
        {
            if (!CantGoRight)
            {
                if (RightStack != null && !RightStack.Fixed)
                {
                    this.RightStack.XPosition++;
                    Score++;
                }
                CurrentPosition_X += 1;
            }

        }

        private void HeroMoveLeft()
        {
            if (!CantGoLeft)
            {
                if (this.LeftStack != null)
                {
                    this.LeftStack.XPosition--;
                    Score++;
                }
                CurrentPosition_X -= 1;
            }

        }
        private void JumpUp()
        {

            CurrentPosition_Y -= 1;
            CantGoLeft = false;
            CantGoRight = false;
            
        }
        /// <summary>
        /// provide a logic to moving hero back, when hero jumped and no other button was pressse
        /// </summary>
        public void BackJump()
        {
            if (localCounter==Constants.STACK_SIZE)
            {
                GoBack = false;
                localCounter = 0;
                Stacked = true;
                IsMoving = false;
            }
            else
            {
                this.CurrentPosition_Y += 1;
                localCounter++;
            }


        }

        #endregion Methods
    }
}
